package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupItemMapper;
import com.itheima.health.dao.CheckGroupMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckGroupItem;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@DubboService
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckGroupItemMapper checkGroupItemMapper;

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        //开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //条件对象
        Example example = new Example(CheckGroup.class);
        Example.Criteria criteria = example.createCriteria();
        //判断查询条件是否为空
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())) {
            criteria.orLike("code", "%" + queryPageBean.getQueryString() + "%");
            criteria.orLike("name", "%" + queryPageBean.getQueryString() + "%");
            criteria.orLike("helpCode", "%" + queryPageBean.getQueryString() + "%");
        }
        Page<CheckGroup> checkGroups = (Page<CheckGroup>) checkGroupMapper.selectByExample(example);

        return new PageResult(checkGroups.getTotal(), checkGroups.getResult());
    }

    @Transactional
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        int i = checkGroupMapper.insertSelective(checkGroup);
        for (Integer checkitemId : checkitemIds) {
            CheckGroupItem checkGroupItem = new CheckGroupItem();
            checkGroupItem.setCheckitemId(checkitemId);
            checkGroupItem.setCheckgroupId(checkGroup.getId());
            checkGroupItemMapper.insertSelective(checkGroupItem);
        }
    }

    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupMapper.selectByPrimaryKey(id);

        return checkGroup;
    }

    @Override
    public List<Integer> findCheckitemIds(Integer id) {
        CheckGroupItem checkGroupItem = new CheckGroupItem();
        checkGroupItem.setCheckgroupId(id);
        List<CheckGroupItem> checkGroupItems = checkGroupItemMapper.select(checkGroupItem);
        List<Integer> integers = new ArrayList<>();
        for (CheckGroupItem groupItem : checkGroupItems) {
            integers.add(groupItem.getCheckitemId());
        }

        return integers;
    }

    @Transactional
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        int i = checkGroupMapper.updateByPrimaryKeySelective(checkGroup);
        CheckGroupItem checkGroupItem = new CheckGroupItem();
        checkGroupItem.setCheckgroupId(checkGroup.getId());
        checkGroupItemMapper.delete(checkGroupItem);
        for (Integer checkitemId : checkitemIds) {
            CheckGroupItem checkGroupItem1 = new CheckGroupItem();
            checkGroupItem1.setCheckgroupId(checkGroup.getId());
            checkGroupItem1.setCheckitemId(checkitemId);
            checkGroupItemMapper.insertSelective(checkGroupItem1);
        }
    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroups = checkGroupMapper.selectAll();
        return checkGroups;
    }
}
